<template>
    <div id="new-info-box">
        <h3>{{newInfo.title}}</h3>

        <p class="time_and_click">
            <span>发表时间：{{newInfo.add_time | dateFormat}}</span>
            <span>点击：{{newInfo.click}}次</span>
        </p>

        <hr>

        <div class="new_content" v-html="newInfo.content">
        </div>

        <comment-box :newId="this.id"></comment-box>
    </div>
</template>

<script>
import { Toast } from "mint-ui" 
import comment from '../subcomponents/comment.vue'

export default {
    data: function(){
        return {
            id: -1,
            newInfo: []
        }
    },
    created(){
        this.getId();
        this.getNewInfo();
    },
    methods: {
        getId(){
            this.id = this.$route.params.id;
        },
        getNewInfo(){
            this.$http.get('api/getnew/'+this.id).then((res) => {
                if(res.body.status === 0){
                    this.newInfo = res.body.message[0]
                    this.correctImg();
                }else{
                    Toast("获取新闻数据失败")
                }
            })
        },
        // https://i03piccdn.sogoucdn.com/b066452546baa0f3
        correctImg(){   //真实接口返回的图片不可用，替换为默认图片
            let content = this.newInfo.content
            this.newInfo.content = content.replace(/((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+jpg/g,"https://i03piccdn.sogoucdn.com/b066452546baa0f3");
        }
    },
    components: {
        'comment-box': comment    //注册子组件
    }
}
</script>


<style lang="less">
    #new-info-box{
        padding: 0 5px;

        h3{
            font-size: 16px;
            color: red;
            text-align: center;
            margin:20px 0;
        }

        .time_and_click{
            display: flex;
            justify-content: space-between;
            font-size: 12px;
            color: rgb(35, 151, 247);
        }

        .new_content{
            p{
                text-indent: 2em;
            }
        }
    }
</style>